package org.swan.web.permission.controller;

import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import org.stream.base.MessageBean;
import org.swan.web.permission.bean.PermissionBean;
import org.swan.web.permission.service.PermissionService;

import java.util.List;

@RestController
@RequestMapping("/permission")
public class PermissionController {
    private final PermissionService permissionService;

    public PermissionController(PermissionService permissionService) {
        this.permissionService = permissionService;
    }

    @GetMapping("/get")
    public MessageBean<PermissionBean> get(@RequestParam("permissionId") long permissionId) {
        return MessageBean.MSG_SUCCESS.setData(permissionService.getPermissionById(permissionId));
    }

    @PostMapping("add")
    public MessageBean<PermissionBean> add(@RequestBody PermissionBean permissionBean) {
        long permissionId = permissionService.addPermission(permissionBean);
        return MessageBean.MSG_SUCCESS.setData(permissionService.getPermissionById(permissionBean.getPermissionId()));
    }

    @PostMapping("update")
    public MessageBean<PermissionBean> update(@RequestBody PermissionBean permissionBean) {
        return MessageBean.MSG_SUCCESS.setData(permissionService.updatePermission(permissionBean));
    }

    @GetMapping("delete")
    public MessageBean<PermissionBean> delete(@RequestParam("permissionId") long permissionId) {
        return MessageBean.MSG_SUCCESS.setData(permissionService.deletePermissionById(permissionId));
    }

    @GetMapping("list")
    public MessageBean<List<PermissionBean>> list(@Param("offset") int offset, @Param("rows") int rows) {
        return MessageBean.MSG_SUCCESS.setData(permissionService.list(offset, rows));
    }
}
